package com.idearworld.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.idearworld.beans.QueryBean;
import com.idearworld.dao.BlogTranslateMapMapper;
import com.idearworld.dao.TranslateDataMapper;
import com.idearworld.model.BlogTranslateMap;
import com.idearworld.model.BlogTranslateMapExample;
import com.idearworld.model.TranslateData;
import com.idearworld.model.TranslateDataExample;
import com.idearworld.service.TranslateDataService;

@Service
public class TranslateDataServiceImpl implements TranslateDataService{

	@Autowired
	private BlogTranslateMapMapper blogTranslateMapMapper;
	
	@Autowired
	private TranslateDataMapper translateDataMapper;
	
	@Override
	@Transactional(propagation=Propagation.REQUIRES_NEW)
	public TranslateData createTranslateData(Integer blogId,
			TranslateData translateData) {

		if(blogId==null || translateData ==null){
			return null;
		}
		this.translateDataMapper.insert(translateData);
        
		BlogTranslateMap record=new BlogTranslateMap();
		record.setBlogId(blogId);
		record.setTranslateId(translateData.getId());
		record.setCreateDate(new Date());
		this.blogTranslateMapMapper.insert(record);
		
		return translateData;
	}

	@Override
	public List<TranslateData> queryTranslateData(QueryBean queryBean) {
		BlogTranslateMapExample example=new BlogTranslateMapExample();
		example.or().andBlogIdEqualTo(queryBean.getBlogId());
		List<BlogTranslateMap> mapList=this.blogTranslateMapMapper.selectByExample(example);
		if(mapList !=null && mapList.size()>0){
			List<Integer> idList=new ArrayList<Integer>();
			for(BlogTranslateMap map:mapList){
				idList.add(map.getTranslateId());
			}
			TranslateDataExample translateDataExample=new TranslateDataExample();
			translateDataExample.or().andIdIn(idList);
			return this.translateDataMapper.selectByExample(translateDataExample);
		}
		return null;
	}


	

}
